<?php

class my extends Controller {
	public function index() {
		$this->session();
		$orgs = new organization();
		$memberOrgs = $orgs->getMemberOrgsWithAdmin( $_SESSION['stud_no'] );
		foreach( $memberOrgs as $org ) {
			$org_id = $org['org_id'];	
			$orgboxes[$org_id]['logo'] = $org['logo'];
			$orgboxes[$org_id]['name'] = $org['name'];
			$orgboxes[$org_id]['description'] = $org['description'];
			$orgboxes[$org_id]['long_description'] = $org['long_description'];
			$orgboxes[$org_id]['org_id'] = $org_id;
			$orgboxes[$org_id]['admin_id'] = $org['admin_id'];
		}
		$content = BufferedView::instance('public/org', array( 'orgboxes' => (array)$orgboxes) )->getBuffer();
		$content .= '<a href="/">View all courses</a>';
		new View('template', 
			array(
				'title' => 'My Courses - iTamCity',
				'heading' => 'My Courses',
				'globalnav' => new BufferedView('org/globalnav'),
				'usernav' => new BufferedView('members/usernav', array( 'highlight' => 'myorgs' )),
				'content' => $content
			)
		);	
	}
	
	public function activities() {
		$this->session();
		$model = new member();
		$event = $model->getMyEvent($_SESSION['stud_no']); 
		new View('template', 
			array(
				'title' => 'My Registered Events - FEELS',
				'heading' => 'My Registered Events',
				'globalnav' => new BufferedView('org/globalnav'),
				'usernav' => new BufferedView('members/usernav', array( 'highlight' => 'myactivities' )),
				'content' => new BufferedView('members/activities',array(
						'activities' => $event
				) )
			)
		);	
	}
	
	public function password($action = NULL) {
		$this->session();
		if( $action !== NULL ) {
			$model = new user();
			$output = '';
			if( $action === 'invalid' ) $output .= '<div>Invalid password</div>';
			if( $action === 'blank' ) $output .= '<div>New password cannot be blank</div>';
			if( $action === 'mismatch' ) $output .= '<div>The two new passwords do not match</div>';
			if( $action === 'confirm' ) {
				if( $_POST['new'] !== $_POST['confirm'] ) header('Location: /my/password/mismatch');
				elseif( empty($_POST['confirm'] ) ) header('Location: /my/password/blank');
				else {
					$update = $model->changePassword( $_SESSION['stud_no'], $_POST['old'], $_POST['new'] );
					if( $update === 0 ) header('Location: /my/password/invalid');
					elseif( $update === 1 ) $output .= 'Password changed.';
				}
			}
		}
		new View('template', 
			array(
				'title' => 'Change Password - FEELS',
				'heading' => 'Change Password',
				'globalnav' => new BufferedView('org/globalnav'),
				'usernav' => new BufferedView('members/usernav', array( 'highlight' => 'mypassword' )),
				'content' => $output . BufferedView::instance('members/forms/password')->getBuffer()
			)
		);			
	}
}